(function() {
  app.controller('myConditionCtrl', function($scope, tradeApi, header_m, $sce, $timeout, $state, alertTip, centerdilog, $window) {
    var tab_type = $state.current.name;
    header_m.set({
      txt_r: '',
      css_r: {
        name: 'refresh@2x'
      },
      fun_r: function() {
        $scope.init();
        $('#header_right').removeClass('reload-ate');
        $timeout(function() {
          $('#header_right').addClass('reload-ate');
        });
      }
    });
    $scope.query = function(data) {
      tradeApi
        .query({
          funcId: '300041',
          data: data
        })
        .then(function(res) {
          $scope.dir_cards = $scope.config_(res);
        });
    };

    $scope.init = function() {
      var data = null;
      if (tab_type === 'myCondition.running') {
        data = { preprocess_status: '0' };
      } else if (tab_type === 'myCondition.trigger') {
        data = { preprocess_status: '1' };
      } else if (tab_type === 'myCondition.invalid') {
        data = { preprocess_status: '2' };
      }

      $scope.query(data);
    };

    $scope.config_ = function(data) {
      var base = {
        data: null,
        config: {
          title: {
            css: {},
            content: { 'key1': 'product_code', 'key2': 'product_name', 'split': '--' },
          },
          rows: {
            css: {
              left: { color: '#333333' },
              right: { color: '#333333' }
            },
            content: null,
          },
          state: {
            show: true,
          }
        },
      };

      base.config.rows.content = [
        { txt: '策略执行金', key: 'borrow_balance', unit: '元', },
        { txt: '履约保证金', key: 'deposit_balance', unit: '元', },
        { txt: '综合服务费', key: 'synthesize_fare', unit: '元', },
        { txt: '条件单类型', key: 'cust_tjd_type' },
        { txt: '条件类型', key: 'cust_tj_type', },
        { txt: '条件详情', key: 'cust_tj_xq' },
        { txt: '生效时点', key: 'entrust_date', key2: 'entrust_time' },
        {card_li_hiden:true},
        {card_li_hiden:true},
        {card_li_hiden:true},
        {card_li_hiden:true},
        { txt: '条件单编号', key: 'serial_no' },
      ];

      base.config.buttons = {
        cancel_txt: '撤销',
        cancel: function(obj) {
          centerdilog
            .open({
              msg: '条件单撤销后立即失效，不可恢复！',
              ok: function() {
                tradeApi
                  .query({
                    funcId: '300042',
                    data: {
                      preprocess_id: obj.obj.serial_no
                    }
                  })
                  .then(function(res) {
                    $scope.init();
                    centerdilog
                      .open({
                        msg: '撤销成功！',
                        ok: function() {},
                        cancel_hiden: false,
                        oktxt: '关闭',
                      });
                  });
              },
            });

        }
      };

      if (tab_type === 'myCondition.trigger') { 
        base.config.rows.content['10'] ={ txt: '执行结果', key: 'valid_date' };
        base.config.rows.content['9'] = { txt: '触发时点', key: 'trigger_time' };
      }else{
        base.config.rows.content['7'] = { txt: '失效时点', key: 'valid_date',key2:'valid_untill_time'};
      }

      angular.forEach(data, function(item) {
        if (item.preprocess_status === '0') {
          item.cancel_button_show = true;
        }
        item.cust_tjd_type = item.preprocess_type === '0' ? '创建策略' : '发出卖出指令';

        if (item.trigger_type === '1') {
          item.cust_tj_type = '价格';
          item.cust_tj_xq = '价格' + (item.trigger_direction === '1' ? '≤' : '≥') + item.efficient_price;
        } else if (item.trigger_type === '2') {
          item.cust_tj_type = '时点';
          item.cust_tj_xq = item.entrust_date + ' ' + item.entrust_time;
        }
      });

      base.data = data;
      return base;
    }


  });
})();